/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Projects
 * Copyright (C) Arnaud Duval 2010 <arnaud.duval@gmail.com>
 * 
 * Projects is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Projects is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "activity-types.hpp"

#include <cassert>
#include <iostream>

//Constructeur
ActivityTypes::ActivityTypes()
{
	nbTypes = 0;
}

//Constructeur par recopie
ActivityTypes::ActivityTypes(const ActivityTypes& a)
{
	nbTypes = a.nbTypes;
	types = new std::string[nbTypes];	
	for(unsigned int i = 0 ; i < nbTypes ; i++)
	{
		types[i] = a.types[i];
	}
}

//Constructeur en précisant la taille
///@param n Nombre de types d'activités
ActivityTypes::ActivityTypes(unsigned int n)
{
	nbTypes = n;
	types = new std::string[n];
}

//Destructeur
ActivityTypes::~ActivityTypes()
{
	if(nbTypes != 0)
		delete[] types;
}

//Retourne l'index d'un type d'activité donné
int ActivityTypes::Index(std::string s)
{
	if(!Exists(s))
		return -1;
	else
	{
		unsigned int i = 0;
		while(i < nbTypes)
		{
			if(types[i] == s)
				return i;
			i++;
		}
	}
}

//Retourne l'intitulé d'une activité en fonction de son index
std::string ActivityTypes::Type(unsigned int i)
{
	assert(i >= 0);
	assert(i < nbTypes);
	return types[i];
}

//Ajoute une nouvell activité
bool ActivityTypes::Add(std::string s)
{
	if(Exists(s))
		return false;
	std::string *temp = new std::string[nbTypes];
	for(unsigned int i = 0 ; i < nbTypes ; i++)
		temp[i] = types[i];
	if(nbTypes != 0)
		delete[] types;
	nbTypes ++;
	types = new std::string[nbTypes];
	for(unsigned int i = 0 ; i < nbTypes-1 ; i++)
		types[i] = temp[i];
	types[nbTypes-1] = s;
	if(nbTypes-1 != 0)
		delete[] temp;
	return true;
}

//vérifie si un intitulé d'activité existe
bool ActivityTypes::Exists(std::string s)
{
	unsigned int i = 0;
	while(i < nbTypes)
	{
		if(types[i] == s)
			return true;
		i++;
	}
	return false;
}

//retourne le nombre de type enregistré
unsigned int ActivityTypes::NbTypes()
{
	return nbTypes;
}

//Surcharge de l'opérateur =
ActivityTypes& ActivityTypes::operator=(const ActivityTypes& a)
{
	nbTypes = a.nbTypes;
	types = new std::string[nbTypes];	
	for(unsigned int i = 0 ; i < nbTypes ; i++)
	{
		types[i] = a.types[i];
	}
	return (*this);
}

///@brief Read informations form a XML node
///@param currentNode Pointer to XML node to read
void ActivityTypes::ReadXMLNode(xmlpp::Node* currentNode)
{
	xmlpp::Node::NodeList listNodes = currentNode->get_children();
	for(xmlpp::Node::NodeList::iterator iterNodes = listNodes.begin(); iterNodes != listNodes.end(); ++iterNodes)
	{
		xmlpp::Node* nodeToAdd = dynamic_cast<const xmlpp::Node*>(*iterNodes);
		if(nodeToAdd->get_name() == "ActivityName")
		///@brief Adding a new activity name
		{
			std::string newActivityName;
			newActivityName = ((xmlpp::TextNode*)(*((nodeToAdd->get_children()).begin())))->get_content();
			Add(newActivityName);
		}
	}
}

///@brief Write to a XML file
///@param fout out stream to XML file
void ActivityTypes::WriteXML(std::ofstream& fout)
{
	fout << "\t<ActivityTypes>\n";
	for(unsigned int i = 0 ; i < nbTypes ; i++)
		fout << "\t\t<ActivityName>" << types[i] <<"</ActivityName>\n";
	fout << "\t</ActivityTypes>\n";
}

///@brief Clear the list of activity types
void ActivityTypes::Clear()
{
	if(nbTypes != 0)
		delete[] types;
	nbTypes = 0;
}